﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class BillingOrderBLL
    {
        private static readonly IBillingCredenceDA billingCredenceDA = DataAccess.CreateBillingCredenceDA();
        private static readonly IBillingEntityDA billingEntityDA = DataAccess.CreateBillingEntityDA();
        private Dictionary<string, BillingEntityInfo> billingEntityInfoObjects;
        private static readonly IBillingOrderDA billingOrderDA = DataAccess.CreateBillingOrderDA();
        private static readonly ICalendarGroupDA calendarGroupDA = DataAccess.CreateCalendarGroupDA();
        private static readonly ICalendarGroupDetailDA calendarGroupDetailDA = DataAccess.CreateCalendarGroupDetailDA();
        private Dictionary<string, List<CalendarGroupDetailInfo>> calendarGroupDetailInfoObjects;
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IMovementDA movementDA = DataAccess.CreateMovementDA();
        private static readonly IMovementDetailDA movementDetailDA = DataAccess.CreateMovementDetailDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private PubParasInfo pubParasInfo;
        private static readonly IRateDA rateDA = DataAccess.CreateRateDA();
        private static readonly IRateDetailDA rateDetailDA = DataAccess.CreateRateDetailDA();
        private Dictionary<string, List<RateDetailInfo>> rateDetailObjects;
        private static readonly IReceiptDA receiptDA = DataAccess.CreateReceiptDA();
        private static readonly IReceiptDetailDA receiptDetailDA = DataAccess.CreateReceiptDetailDA();
        private static readonly IReceiptDetailLpnDA receiptDetailLpnDA = DataAccess.CreateReceiptDetailLpnDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private Dictionary<string, SkuInfo> skuInfoObjects;
        private static readonly ITariffDA tariffDA = DataAccess.CreateTariffDA();
        private static readonly ITariffDetailDA tariffDetailDA = DataAccess.CreateTariffDetailDA();
        private Dictionary<string, TariffInfo> tariffInfoObjects;

        public BillingOrderBLL()
        {
            this.pubParasInfo = null;
            this.skuInfoObjects = new Dictionary<string, SkuInfo>();
            this.tariffInfoObjects = new Dictionary<string, TariffInfo>();
            this.rateDetailObjects = new Dictionary<string, List<RateDetailInfo>>();
            this.billingEntityInfoObjects = new Dictionary<string, BillingEntityInfo>();
            this.calendarGroupDetailInfoObjects = new Dictionary<string, List<CalendarGroupDetailInfo>>();
        }

        public BillingOrderBLL(string currentWH)
        {
            this.pubParasInfo = null;
            this.skuInfoObjects = new Dictionary<string, SkuInfo>();
            this.tariffInfoObjects = new Dictionary<string, TariffInfo>();
            this.rateDetailObjects = new Dictionary<string, List<RateDetailInfo>>();
            this.billingEntityInfoObjects = new Dictionary<string, BillingEntityInfo>();
            this.calendarGroupDetailInfoObjects = new Dictionary<string, List<CalendarGroupDetailInfo>>();
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public BillingOrderBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            this.skuInfoObjects = new Dictionary<string, SkuInfo>();
            this.tariffInfoObjects = new Dictionary<string, TariffInfo>();
            this.rateDetailObjects = new Dictionary<string, List<RateDetailInfo>>();
            this.billingEntityInfoObjects = new Dictionary<string, BillingEntityInfo>();
            this.calendarGroupDetailInfoObjects = new Dictionary<string, List<CalendarGroupDetailInfo>>();
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string GenerateBillingCredence(List<string> docID, List<string> docType)
        {
            string temp = string.Empty;
            for (int i = 0; i < docID.Count; i++)
            {
                DataBase dataBase;
                List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
                string CS40001 = docType[i];
                if (CS40001 != null)
                {
                    if (!(CS40001 == "InBound"))
                    {
                        if (CS40001 == "OutBound")
                        {
                            goto Label_006C;
                        }
                        if (CS40001 == "Movement")
                        {
                            goto Label_007C;
                        }
                        if (CS40001 == "Vas")
                        {
                            goto Label_008C;
                        }
                    }
                    else
                    {
                        billingCredenceListInfo = this.GetInboundBillingCredenceListInfo(docID[i]);
                    }
                }
                goto Label_009C;
            Label_006C:
                billingCredenceListInfo = this.GetOutboundBillingCredenceListInfo(docID[i]);
                goto Label_009C;
            Label_007C:
                billingCredenceListInfo = this.GetMovementBillingCredenceListInfo(docID[i]);
                goto Label_009C;
            Label_008C:
                billingCredenceListInfo = this.GetVasBillingCredenceListInfo(docID[i]);
            Label_009C:
                dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    List<OrderDetailInfo> orderDetailInfo;
                    BillingOrderInfo billingOrderInfo = billingOrderDA.GetBillingOrderByOrderIDAndOrderType(dataBase, tran, docID[i], docType[i], this.dataBaseID[0], this.pubParasInfo);
                    if (billingCredenceDA.CheckBillingCredenceByTypeAndID(dataBase, tran, docType[i], docID[i], docType[i], this.dataBaseID[0]) || (billingOrderInfo == null))
                    {
                        goto Label_04A0;
                    }
                    int result = 0;
                    if ((billingCredenceListInfo != null) && (billingCredenceListInfo.Count > 0))
                    {
                        result = billingCredenceDA.InsertBillingCredence(dataBase, tran, billingCredenceListInfo, this.dataBaseID[0]);
                    }
                    else
                    {
                        result = 10;
                    }
                    if (result <= 0)
                    {
                        goto Label_0488;
                    }
                    string status = "Closed";
                    CS40001 = docType[i];
                    if (CS40001 != null)
                    {
                        if (!(CS40001 == "InBound"))
                        {
                            if (CS40001 == "OutBound")
                            {
                                goto Label_02B8;
                            }
                            if (CS40001 == "Movement")
                            {
                                goto Label_0453;
                            }
                            if (CS40001 == "Vas")
                            {
                            }
                        }
                        else
                        {
                            receiptDA.UpdateReceiptStatus(dataBase, tran, docID[i], status, this.dataBaseID[0], this.pubParasInfo.UserID);
                            if (receiptDA.GetReceiptByID(dataBase, tran, docID[i], this.dataBaseID[0], this.pubParasInfo).IsPalletized == "Y")
                            {
                                receiptDetailLpnDA.UpdateReceiptDetailLpnStatus(dataBase, tran, docID[i], status, this.pubParasInfo.UserID, this.dataBaseID[0]);
                            }
                            else
                            {
                                receiptDetailDA.UpdateReceiptDetailStatus(dataBase, tran, docID[i], status, this.pubParasInfo.UserID, this.dataBaseID[0]);
                            }
                        }
                    }
                    goto Label_04B5;
                Label_02B8:
                    orderDetailInfo = orderDetailDA.GetOrderDetailListByOrderID(dataBase, tran, docID[i], this.dataBaseID[0], this.pubParasInfo);
                    if ((orderDetailInfo != null) && (orderDetailInfo.Count > 0))
                    {
                        foreach (OrderDetailInfo info in orderDetailInfo)
                        {
                            if (info.PreallocatedQty > 0M)
                            {
                                throw new Exception(" 行号: " + info.LineID + " 已有预分配数量!");
                            }
                            if (info.AllocatedQty > 0M)
                            {
                                throw new Exception(" 行号: " + info.LineID + " 已有分配数量!");
                            }
                            if (info.PickedQty > 0M)
                            {
                                throw new Exception(" 行号: " + info.LineID + " 已有拣货数量!");
                            }
                        }
                    }
                    orderDetailDA.UpdateOrderDetailStatus(dataBase, tran, docID[i], OrderStatus.Closed, this.pubParasInfo.UserID, this.dataBaseID[0]);
                    ordersDA.UpdateOrdersStatus(dataBase, tran, docID[i], status, this.pubParasInfo.UserID, this.dataBaseID[0]);
                    goto Label_04B5;
                Label_0453:
                    movementDA.UpdateMovementStatus2(dataBase, tran, status, docID[i], this.pubParasInfo.UserID, this.dataBaseID[0]);
                    goto Label_04B5;
                Label_0488:
                    temp = temp + docID[i] + ",";
                    goto Label_04B5;
                Label_04A0:
                    temp = temp + docID[i] + ",";
                Label_04B5:
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    string CS00004 = temp;
                    temp = CS00004 + docID[i] + ":" + ex.Message + ";";
                }
                finally
                {
                    conn.Close();
                }
            }
            return temp;
        }

        public void GenerateBillingCredenceByCloseOrder(DataBase dataBase, DbTransaction tran, string docID, string docType, List<BillingCredenceInfo> billingCredenceListInfo)
        {
            if (!billingCredenceDA.CheckBillingCredenceByTypeAndID(dataBase, tran, docType, docID, docType, this.dataBaseID[0]) && ((billingCredenceListInfo != null) && (billingCredenceListInfo.Count > 0)))
            {
                billingCredenceDA.InsertBillingCredence(dataBase, tran, billingCredenceListInfo, this.dataBaseID[0]);
            }
        }

        public DataSet GetAllBillingOrderInfo()
        {
            return billingOrderDA.GetAllBillingOrder(this.dataBaseID[0], this.pubParasInfo);
        }

        private List<BillingCredenceInfo> GetBillingCredenceListInfo(List<BillingCredenceInfo> billingCredenceListInfo, string billingType, string billingObject, List<BillingOrderDetailInfo> billingOrderDetailListInfo)
        {
            string whLoginID = this.dataBaseID[0];
            for (int i = 0; i < billingOrderDetailListInfo.Count; i++)
            {
                int j;
                string ownerID = billingOrderDetailListInfo[i].OwnerID;
                string skuID = billingOrderDetailListInfo[i].SkuID;
                List<RateDetailInfo> rateDetailListInfo = null;
                SkuInfo skuInfo = null;
                if (!this.skuInfoObjects.ContainsKey(ownerID + skuID))
                {
                    skuInfo = skuDA.GetSkuByID(ownerID, skuID, whLoginID);
                    if (skuInfo != null)
                    {
                        this.skuInfoObjects.Add(ownerID + skuID, skuInfo);
                    }
                }
                else
                {
                    this.skuInfoObjects.TryGetValue(ownerID + skuID, out skuInfo);
                }
                if ((skuInfo != null) && !string.IsNullOrEmpty(skuInfo.TariffID))
                {
                    if (!this.rateDetailObjects.ContainsKey(ownerID + skuID))
                    {
                        string rateID = string.Empty;
                        TariffInfo tariffInfo = tariffDA.GetTariffByID(skuInfo.TariffID, whLoginID);
                        if (tariffInfo != null)
                        {
                            if (tariffInfo.TariffType == "Fixed")
                            {
                                rateID = tariffInfo.TypeCode;
                            }
                            else if (tariffInfo.TariffType == "CalendarGroup")
                            {
                                List<CalendarGroupDetailInfo> calendarGroupDetailListInfo = null;
                                if (!this.calendarGroupDetailInfoObjects.ContainsKey(tariffInfo.TypeCode))
                                {
                                    calendarGroupDetailListInfo = calendarGroupDetailDA.GetCalendarGroupDetailByID(tariffInfo.TypeCode, whLoginID);
                                    if (calendarGroupDetailListInfo != null)
                                    {
                                        this.calendarGroupDetailInfoObjects.Add(tariffInfo.TypeCode, calendarGroupDetailListInfo);
                                    }
                                }
                                else
                                {
                                    this.calendarGroupDetailInfoObjects.TryGetValue(tariffInfo.TypeCode, out calendarGroupDetailListInfo);
                                }
                                if ((calendarGroupDetailListInfo != null) && (calendarGroupDetailListInfo.Count > 0))
                                {
                                    DateTime dt = DateTime.Now;
                                    j = 0;
                                    while (j < calendarGroupDetailListInfo.Count)
                                    {
                                        if ((dt >= calendarGroupDetailListInfo[j].StartTime) && (dt <= calendarGroupDetailListInfo[j].EndTime))
                                        {
                                            rateID = calendarGroupDetailListInfo[j].RateID;
                                            break;
                                        }
                                        j++;
                                    }
                                }
                            }
                            rateDetailListInfo = rateDetailDA.GetRateDetailByID(rateID, whLoginID);
                            if ((rateDetailListInfo != null) && (rateDetailListInfo.Count > 0))
                            {
                                this.rateDetailObjects.Add(ownerID + skuID, rateDetailListInfo);
                            }
                        }
                    }
                    else
                    {
                        this.rateDetailObjects.TryGetValue(ownerID + skuID, out rateDetailListInfo);
                    }
                }
                if ((rateDetailListInfo != null) && (rateDetailListInfo.Count > 0))
                {
                    List<RateDetailInfo> billingRateDetailListInfo = rateDetailListInfo.FindAll(rateDetailInfo => rateDetailInfo.BillingObject == billingObject).FindAll(rateDetailInfo => rateDetailInfo.RateType == billingType);
                    if ((billingRateDetailListInfo != null) && (billingRateDetailListInfo.Count > 0))
                    {
                        for (j = 0; j < billingRateDetailListInfo.Count; j++)
                        {
                            decimal total;
                            BillingCredenceInfo billingCredenceInfo;
                            decimal fixRate = 0M;
                            if (billingRateDetailListInfo[j].ChargeMode == "Fixed")
                            {
                                fixRate = billingRateDetailListInfo[j].FixedRate;
                            }
                            else
                            {
                                if (!(billingRateDetailListInfo[j].ChargeMode == "ChargeUnitLevel"))
                                {
                                    goto Label_06E7;
                                }
                                total = 0M;
                                switch (billingRateDetailListInfo[j].ChargeUnit)
                                {
                                    case "Weight":
                                        total = decimal.Parse(skuInfo.GrossWgt.ToString()) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070A;

                                    case "Measurement":
                                        total = decimal.Parse(skuInfo.TareWgt.ToString()) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070A;

                                    case "AMT":
                                        total = decimal.Parse(skuInfo.CostPrice.ToString()) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070A;

                                    case "Area":
                                        total = (decimal.Parse(skuInfo.Length.ToString()) * decimal.Parse(skuInfo.Width.ToString())) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070A;

                                    case "Quantity":
                                        total = billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070A;

                                    case "BillingTon":
                                        if (skuInfo.GrossWgt < skuInfo.Cube)
                                        {
                                            goto Label_0646;
                                        }
                                        total = decimal.Parse(skuInfo.GrossWgt.ToString()) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070A;

                                    case "Pallet":
                                    case "Location":
                                        goto Label_070A;

                                    case "FixedRate":
                                        fixRate = billingRateDetailListInfo[j].FixedRate;
                                        goto Label_070A;

                                    case "BillingPallet":
                                        total = billingOrderDetailListInfo[i].Pallet;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070A;

                                    case "BillingCase":
                                        total = billingOrderDetailListInfo[i].Case;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070A;
                                }
                            }
                            goto Label_070A;
                        Label_0646:
                            total = decimal.Parse(skuInfo.Cube.ToString()) * billingOrderDetailListInfo[i].Qty;
                            fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                            goto Label_070A;
                        Label_06E7:
                            if (billingRateDetailListInfo[j].ChargeMode == "StcTimeLevel")
                            {
                            }
                        Label_070A:
                            if ((!(fixRate != 0M) || !(billingRateDetailListInfo[j].ChargeUnit != "Pallet")) || !(billingRateDetailListInfo[j].ChargeUnit != "Location"))
                            {
                                goto Label_0E80;
                            }
                            billingCredenceInfo = new BillingCredenceInfo();
                            billingCredenceInfo.WhID = this.pubParasInfo.CurrentWH;
                            billingCredenceInfo.BillingCredenceID = commonMethod.GenNextNumStr("BILLING_CREDENCE", whLoginID);
                            billingCredenceInfo.CredenceType = billingOrderDetailListInfo[i].BillingType;
                            billingCredenceInfo.DocID = billingOrderDetailListInfo[i].DocID;
                            billingCredenceInfo.ExternalDocID = billingOrderDetailListInfo[i].ExternalDocID;
                            billingCredenceInfo.DocType = billingOrderDetailListInfo[i].BillingType;
                            billingCredenceInfo.DocLineID = 0;
                            billingCredenceInfo.OwnerID = billingOrderDetailListInfo[i].OwnerID;
                            billingCredenceInfo.SkuID = billingOrderDetailListInfo[i].SkuID;
                            billingCredenceInfo.NetWeight = decimal.Parse(skuInfo.NetWgt.ToString());
                            billingCredenceInfo.Volume = decimal.Parse(skuInfo.Cube.ToString());
                            billingCredenceInfo.GrossWeight = decimal.Parse(skuInfo.GrossWgt.ToString());
                            billingCredenceInfo.TotalNetWeight = billingCredenceInfo.NetWeight * billingOrderDetailListInfo[i].Qty;
                            billingCredenceInfo.TotalVolume = billingCredenceInfo.Volume * billingOrderDetailListInfo[i].Qty;
                            billingCredenceInfo.TotalGrossWeight = billingCredenceInfo.GrossWeight * billingOrderDetailListInfo[i].Qty;
                            if (billingObject == "BillingEntity")
                            {
                                BillingEntityInfo billingEntityInfo = new BillingEntityInfo();
                                if (!this.billingEntityInfoObjects.ContainsKey(billingRateDetailListInfo[j].BillingEntity))
                                {
                                    billingEntityInfo = billingEntityDA.GetBillingEntityByID(billingRateDetailListInfo[j].BillingEntity, whLoginID, this.pubParasInfo);
                                    if (billingEntityInfo != null)
                                    {
                                        this.billingEntityInfoObjects.Add(billingRateDetailListInfo[j].BillingEntity, billingEntityInfo);
                                    }
                                }
                                else
                                {
                                    this.billingEntityInfoObjects.TryGetValue(billingRateDetailListInfo[j].BillingEntity, out billingEntityInfo);
                                }
                                if (billingEntityInfo != null)
                                {
                                    billingCredenceInfo.BillingObjectName = billingEntityInfo.Company;
                                }
                                billingCredenceInfo.BillingObjectID = billingRateDetailListInfo[j].BillingEntity;
                            }
                            else
                            {
                                billingCredenceInfo.BillingObjectID = billingOrderDetailListInfo[i].BillingObjectID;
                                billingCredenceInfo.BillingObjectName = billingOrderDetailListInfo[i].BillingObjectName;
                            }
                            billingCredenceInfo.BillingObject = billingObject;
                            billingCredenceInfo.RateType = billingRateDetailListInfo[j].RateType;
                            billingCredenceInfo.CostType = billingRateDetailListInfo[j].Descr;
                            billingCredenceInfo.DueFromTo = billingRateDetailListInfo[j].DueFromTo;
                            billingCredenceInfo.ChargeMode = billingRateDetailListInfo[j].ChargeMode;
                            billingCredenceInfo.ChargeUnit = billingRateDetailListInfo[j].ChargeUnit;
                            billingCredenceInfo.FixedRate = billingRateDetailListInfo[j].FixedRate;
                            billingCredenceInfo.CreatedBy = this.pubParasInfo.UserID;
                            billingCredenceInfo.UpdatedBy = this.pubParasInfo.UserID;
                            billingCredenceInfo.Quantity = billingOrderDetailListInfo[i].Qty;
                            if (billingRateDetailListInfo[j].ChargePerUnit <= 0M)
                            {
                                billingRateDetailListInfo[j].ChargePerUnit = 1M;
                            }
                            switch (billingRateDetailListInfo[j].ChargeUnit)
                            {
                                case "Weight":
                                    billingCredenceInfo.BillingAmount = (billingCredenceInfo.TotalGrossWeight * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E76;

                                case "Measurement":
                                    billingCredenceInfo.BillingAmount = (billingCredenceInfo.TotalVolume * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E76;

                                case "AMT":
                                    billingCredenceInfo.BillingAmount = ((billingCredenceInfo.Quantity * skuInfo.CostPrice) * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E76;

                                case "Area":
                                    billingCredenceInfo.BillingAmount = (((billingCredenceInfo.Quantity * decimal.Parse(skuInfo.Width.ToString())) * decimal.Parse(skuInfo.Length.ToString())) * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E76;

                                case "Quantity":
                                    billingCredenceInfo.BillingAmount = (billingCredenceInfo.Quantity * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E76;

                                case "BillingTon":
                                    if (skuInfo.GrossWgt < skuInfo.Cube)
                                    {
                                        break;
                                    }
                                    billingCredenceInfo.BillingAmount = (billingCredenceInfo.TotalGrossWeight * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E76;

                                case "Pallet":
                                    billingCredenceInfo.BillingAmount = 0M;
                                    goto Label_0E76;

                                case "Location":
                                    billingCredenceInfo.BillingAmount = 0M;
                                    goto Label_0E76;

                                case "FixedRate":
                                    billingCredenceInfo.BillingAmount = billingCredenceInfo.FixedRate;
                                    goto Label_0E76;

                                case "BillingPallet":
                                    billingCredenceInfo.Quantity = billingOrderDetailListInfo[i].Pallet;
                                    billingCredenceInfo.BillingAmount = (billingOrderDetailListInfo[i].Pallet * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E76;

                                case "BillingCase":
                                    billingCredenceInfo.Quantity = billingOrderDetailListInfo[i].Case;
                                    billingCredenceInfo.BillingAmount = (billingOrderDetailListInfo[i].Case * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E76;

                                default:
                                    goto Label_0E76;
                            }
                            billingCredenceInfo.BillingAmount = (billingCredenceInfo.TotalVolume * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                        Label_0E76:
                            billingCredenceListInfo.Add(billingCredenceInfo);
                        Label_0E80:;
                        }
                    }
                }
            }
            return billingCredenceListInfo;
        }

        public DataSet GetBillingOrderByQueryList(BillingOrderQueryEntity billingOrderQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(billingOrderQuery.DocID))
            {
                sqlWhere.Add(" ORDERS.DOC_ID " + billingOrderQuery.DocID);
            }
            if (!string.IsNullOrEmpty(billingOrderQuery.DocType))
            {
                sqlWhere.Add(" ORDERS.DOC_TYPE " + billingOrderQuery.DocType);
            }
            if (!string.IsNullOrEmpty(billingOrderQuery.OwnerID))
            {
                sqlWhere.Add(" ORDERS.OWNER_ID " + billingOrderQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(billingOrderQuery.IsPalletInput))
            {
                sqlWhere.Add(" INV_PALLET.CASED " + billingOrderQuery.IsPalletInput);
            }
            if (!string.IsNullOrEmpty(billingOrderQuery.IsPalletAudit))
            {
                sqlWhere.Add(" ORDERS.IS_PALLET_AUDIT " + billingOrderQuery.IsPalletAudit);
            }
            if (!string.IsNullOrEmpty(billingOrderQuery.CreatedBy))
            {
                sqlWhere.Add(" ORDERS.CREATED_BY " + billingOrderQuery.CreatedBy);
            }
            if (!string.IsNullOrEmpty(billingOrderQuery.CreatedDate))
            {
                sqlWhere.Add(billingOrderQuery.CreatedDate.Replace("@CreatedDate@", " ORDERS.CREATED_DATE "));
            }
            return billingOrderDA.GetBillingOrderByQueryList(sqlWhere, billingOrderQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<BillingCredenceInfo> GetInboundBillingCredenceListInfo(string docID)
        {
            string whLoginID = this.dataBaseID[0];
            List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
            List<BillingOrderDetailInfo> billingOrderDetailListInfo = null;
            billingOrderDetailListInfo = receiptDetailDA.GetReceiptBillingOrderDetailListInfo(docID, "Owner", this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "InBound", "Owner", billingOrderDetailListInfo);
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "InBound", "BillingEntity", billingOrderDetailListInfo);
            }
            billingOrderDetailListInfo = receiptDetailDA.GetReceiptBillingOrderDetailListInfo(docID, "Vendor", this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "InBound", "Vendor", billingOrderDetailListInfo);
            }
            billingOrderDetailListInfo = receiptDetailDA.GetReceiptBillingOrderDetailListInfo(docID, "Carrier", this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "InBound", "Carrier", billingOrderDetailListInfo);
            }
            return billingCredenceListInfo;
        }

        public List<BillingCredenceInfo> GetMovementBillingCredenceListInfo(string docID)
        {
            string whLoginID = this.dataBaseID[0];
            List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
            List<BillingOrderDetailInfo> billingOrderDetailListInfo = null;
            billingOrderDetailListInfo = movementDetailDA.GetMovementBillingOrderDetailListInfo(docID, "Owner", this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "Movement", "Owner", billingOrderDetailListInfo);
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "Movement", "BillingEntity", billingOrderDetailListInfo);
            }
            billingOrderDetailListInfo = movementDetailDA.GetMovementBillingOrderDetailListInfo(docID, "Vendor", this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "Movement", "Vendor", billingOrderDetailListInfo);
            }
            return billingCredenceListInfo;
        }

        public List<BillingCredenceInfo> GetOutboundBillingCredenceListInfo(string docID)
        {
            string whLoginID = this.dataBaseID[0];
            List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
            List<BillingOrderDetailInfo> billingOrderDetailListInfo = null;
            billingOrderDetailListInfo = orderDetailDA.GetOrderBillingOrderDetailListInfo(docID, "Owner", this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "OutBound", "Owner", billingOrderDetailListInfo);
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "OutBound", "BillingEntity", billingOrderDetailListInfo);
            }
            billingOrderDetailListInfo = orderDetailDA.GetOrderBillingOrderDetailListInfo(docID, "Customer", this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "OutBound", "Customer", billingOrderDetailListInfo);
            }
            billingOrderDetailListInfo = orderDetailDA.GetOrderBillingOrderDetailListInfo(docID, "Vendor", this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "OutBound", "Vendor", billingOrderDetailListInfo);
            }
            billingOrderDetailListInfo = orderDetailDA.GetOrderBillingOrderDetailListInfo(docID, "Carrier", this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "OutBound", "Carrier", billingOrderDetailListInfo);
            }
            return billingCredenceListInfo;
        }

        private decimal GetRateByChargeUnit(decimal total, RateDetailInfo rateDetailInfo)
        {
            if ((total >= rateDetailInfo.CucFrom01) && (total <= rateDetailInfo.CucTo01))
            {
                return rateDetailInfo.CucRate01;
            }
            if ((total >= rateDetailInfo.CucFrom02) && (total <= rateDetailInfo.CucTo02))
            {
                return rateDetailInfo.CucRate02;
            }
            if ((total >= rateDetailInfo.CucFrom03) && (total <= rateDetailInfo.CucTo03))
            {
                return rateDetailInfo.CucRate03;
            }
            if ((total >= rateDetailInfo.CucFrom04) && (total <= rateDetailInfo.CucTo04))
            {
                return rateDetailInfo.CucRate04;
            }
            if ((total >= rateDetailInfo.CucFrom05) && (total <= rateDetailInfo.CucTo05))
            {
                return rateDetailInfo.CucRate05;
            }
            if ((total >= rateDetailInfo.CucFrom06) && (total <= rateDetailInfo.CucTo06))
            {
                return rateDetailInfo.CucRate06;
            }
            if ((total >= rateDetailInfo.CucFrom07) && (total <= rateDetailInfo.CucTo07))
            {
                return rateDetailInfo.CucRate07;
            }
            if ((total >= rateDetailInfo.CucFrom08) && (total <= rateDetailInfo.CucTo08))
            {
                return rateDetailInfo.CucRate08;
            }
            if ((total >= rateDetailInfo.CucFrom09) && (total <= rateDetailInfo.CucTo09))
            {
                return rateDetailInfo.CucRate09;
            }
            if ((total >= rateDetailInfo.CucFrom10) && (total <= rateDetailInfo.CucTo10))
            {
                return rateDetailInfo.CucRate10;
            }
            return 0M;
        }

        public List<BillingCredenceInfo> GetVasBillingCredenceListInfo(string docID)
        {
            return null;
        }
    }
}

